local skel = fk.CreateSkill {
  name = "emo__yujiang",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__yujiang"] = "御江",
  [":emo__yujiang"] = "锁定技，当你使用本回合被弃置过花色的牌后，摸一张牌。",
  ["@emo__yujiang-turn"] = "御江",

  ["$emo__yujiang1"] = "腰悬三江，肩担六山，衡方圆于乱世。",
  ["$emo__yujiang2"] = "坐断东南，挥斥宇内，彼以何论英雄？",
}

skel:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and
    table.contains(player:getTableMark("@emo__yujiang-turn"), data.card:getSuitString(true))
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, skel.name)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local suits = {}
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            local card = Fk:getCardById(info.cardId)
            if card.suit ~= Card.NoSuit then
              table.insertIfNeed(suits, card:getSuitString(true))
            end
          end
        end
      end
    end
    if #suits == 0 then return end
    local mark = player:getTableMark("@emo__yujiang-turn")
    table.insertTableIfNeed(mark, suits)
    player.room:setPlayerMark(player, "@emo__yujiang-turn", mark)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__yujiang-turn", 0)
end)

skel:addAcquireEffect(function (self, player, is_start)
  local suits = {}
  player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
    for _, move in ipairs(e.data) do
      if move.from == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip or info.fromArea == Card.PlayerHand then
            local card = Fk:getCardById(info.cardId)
            if card.suit ~= Card.NoSuit then
              table.insertIfNeed(suits, card:getSuitString(true))
            end
          end
        end
      end
      if #suits == 4 then return true end
    end
  end, Player.HistoryTurn)
  if #suits == 0 then return end
  player.room:setPlayerMark(player, "@emo__yujiang-turn", suits)
end)

return skel
